package com.gxkj.movie.controllers;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gxkj.movie.controllers.seo.BaseSeo;
import com.gxkj.movie.helpers.NewsRecommendHelpper;
import com.gxkj.movie.helpers.ProShopHelper;
import com.gxkj.movie.helpers.VodRecommendHelpper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author sqyc
 */
@Component
@Slf4j
public abstract class BaseController {

    public void renderHeader(HttpServletRequest req, Model model){
        model.addAttribute("siteName", BaseSeo.SITE_NAME);
    }

    public String getTempletate(HttpServletRequest req){

//        if(isMobileDevice(req)){
//            return "mobile";
//        }
        return "/pc";
    }

    public static boolean  isMobileDevice(HttpServletRequest request){
        /**
         * android : 所有android设备
         * mac os : iphone ipad
         * windows phone:Nokia等windows系统的手机
         */

//        iPhone|iPod|Android|ios|iPad
        String userAgent = request.getHeader("user-agent");
        if(StringUtils.isNotEmpty(userAgent)){
            userAgent = userAgent.toLowerCase();
            if (userAgent.indexOf("iphone") != -1 || userAgent.indexOf("ipad") != -1 || userAgent.indexOf("android") != -1){
                return true;
            }
        }


        return false;
    }


    protected  static int clckTime = 0;

    protected  static JSONArray  dataList = new JSONArray();


    protected static Map<String,JSONArray> cache = new HashMap<>();

    protected static Map<String,Object> timeCache = new HashMap<>();


    /**
     * 右侧推荐信息
     */
    protected static JSONArray  recomentList = new JSONArray();



    /**
     * 推荐电影
     */
    protected static JSONArray  recomentMovList = new JSONArray();

    @Autowired
    protected NewsRecommendHelpper newsRecommendHelpper;

    @Autowired
    protected ProShopHelper proShopHelper;
    /**
     * 新闻期望是10分钟一更新，目前设置为一天已更新吧
     */
    protected static JSONArray recomentNewsList = new JSONArray();


    @Autowired
    private VodRecommendHelpper vodRecommendHelpper;


    public void  renderNews(HttpServletRequest req, Model model, String key){

        if(recomentNewsList == null || recomentNewsList.size() == 0){
            JSONObject pager = newsRecommendHelpper.findRecommendNewsData(1,6,null);

            if(pager != null) {
                recomentNewsList = pager.getJSONArray("list");
            }
        }
        model.addAttribute("recomentNewsList",recomentNewsList);
    }
    /**
     * jd_pro,jd_huodong,taobao_prod
     * @param req
     * @param model
     * @param fromsite
     */
    public void  renderJdProd(HttpServletRequest req, Model model, String fromsite, String title){
        JSONArray list = null;

        if(cache.get(fromsite) == null){
            synchronized (this){
                if(cache.get(fromsite) == null){
                    JSONObject pager = proShopHelper.findProShopPageData(1,10,fromsite);
                    log.info( JSON.toJSONString(pager));
                    if(pager != null) {
                        list = pager.getJSONArray("list");
                    }
                    cache.put(fromsite,list);

                    JSONObject listobj = new JSONObject();
                    listobj.put("title",title);
                    listobj.put("fromsite",fromsite);
                    listobj.put("dataList",list);

                    recomentList.add(listobj);

                }
            }

        }
        model.addAttribute("recomentList",recomentList);

    }

    public void  renderRecommendVod(HttpServletRequest req, Model model){


        if(recomentMovList == null || recomentMovList.size() == 0){

            JSONObject pager = vodRecommendHelpper.findRecommendVodData(1,6,null);
            log.info( JSON.toJSONString(pager));
            if(pager != null) {
                recomentMovList = pager.getJSONArray("list");
            }
        }
        model.addAttribute("recomentMovList",recomentMovList);

    }

    public void cacheContol( HttpServletRequest req, Model model,Boolean forceclear ){
        if(forceclear){
            cache.clear();
            dataList.clear();
            timeCache.clear();
            recomentList.clear();
            recomentMovList.clear();
            recomentNewsList.clear();
            return;
        }
        Date date = new Date();
        String szDate = String.format("%tY-%tm-%td", date,date,date);
        if(timeCache.get(szDate) == null){
            cache.clear();
            dataList.clear();
            timeCache.clear();
            recomentList.clear();
            recomentMovList.clear();
            recomentNewsList.clear();
            timeCache.put(szDate,"TRUE");
        }else {
            clckTime ++;

            if(clckTime % 50 == 0){
                cache.clear();
                dataList.clear();
                recomentList.clear();
                timeCache.clear();
                recomentMovList.clear();
                recomentNewsList.clear();
                timeCache.put(szDate,"TRUE");

            }
        }
    }

    public enum MenuEnum{

        //多个个枚举值,注意名字并不是构造方法名
        INDEX("INDEX","首页"),DevShare("DevShare","开发者共享"),DAZHUANPAN("DAZHUANPAN","大转盘")
        ,ENGLISH("ENGLISH","英语角")
        ,JILUPIAN("JILUPIAN","纪录片")
        ,ZMT("ZMT","自媒体")
        ,CMS("CMS","文章自媒体")
        ,GXZW("GXZW","高薪职位")
        ,VIP_VIDEO("VIP_VIDEO","视频解析")
        ,CUSTOMPERPLAY("CUSTOMPERPLAY","自定义播放");

        //枚举值所包含的属性
        private String key;
        private String value;

        //构造方法
        MenuEnum(String key,String value){
            this.setKey(key);
            this.setValue(value);
        }
        public String getKey() {
            return key;
        }
        public void setKey(String key) {
            this.key = key;
        }
        public String getValue() {
            return value;
        }
        public void setValue(String value) {
            this.value = value;
        }
    }




}
